import { notFound } from 'next/navigation'
import { getTools } from '@/lib/database'

// 强制动态渲染，避免构建时数据库连接
export const dynamic = 'force-dynamic'

interface SimpleToolPageProps {
  params: Promise<{
    id: string
  }>
}

export default async function SimpleToolPage({ params }: SimpleToolPageProps) {
  const { id } = await params

  // 构建时跳过数据库查询
  if (process.env.NODE_ENV === 'development' && process.env.NEXT_PHASE === 'phase-production-build') {
    return (
      <div className="min-h-screen bg-gray-50 py-12">
        <div className="max-w-4xl mx-auto px-4 text-center">
          <h1 className="text-2xl font-bold text-yellow-600 mb-4">构建时跳过</h1>
          <p className="text-gray-600">工具ID: {id}</p>
        </div>
      </div>
    )
  }

  const tools = await getTools()
  const tool = tools.find(t => t.id === id)

  if (!tool) {
    return (
      <div className="min-h-screen bg-gray-50 py-12">
        <div className="max-w-4xl mx-auto px-4 text-center">
          <h1 className="text-2xl font-bold text-red-600 mb-4">工具未找到</h1>
          <p className="text-gray-600 mb-4">ID: {id}</p>
          <p className="text-gray-600">可用的工具ID:</p>
          <ul className="text-sm text-gray-500 mt-2">
            {tools.map(t => (
              <li key={t.id}>{t.id} - {t.title}</li>
            ))}
          </ul>
        </div>
      </div>
    )
  }

  return (
    <div className="min-h-screen bg-gray-50 py-12">
      <div className="max-w-4xl mx-auto px-4">
        <div className="bg-white rounded-lg shadow p-6">
          <h1 className="text-2xl font-bold mb-4">{tool.title}</h1>
          <p className="text-gray-600 mb-4">{tool.description}</p>
          
          <div className="grid grid-cols-2 gap-4 text-sm">
            <div>
              <strong>ID:</strong> {tool.id}
            </div>
            <div>
              <strong>状态:</strong> {tool.status}
            </div>
            <div>
              <strong>分类:</strong> {tool.category?.name}
            </div>
            <div>
              <strong>创建者:</strong> {tool.user?.name}
            </div>
          </div>

          <div className="mt-6">
            <a 
              href={tool.url}
              target="_blank"
              rel="noopener noreferrer"
              className="inline-block px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600"
            >
              访问工具
            </a>
          </div>
        </div>
      </div>
    </div>
  )
}
